package com.example.jason.myapplication;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private SpringView springView;
    private NotificationManager nm;
    private Notification notification;
    private RemoteViews remoteViews;
    private int notificationId = 1234;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //测试测试master分支的
        //dev 测试
//        PullToRefreshListView pullToRefreshListView = findViewById(R.id.lv_pullRefresh);
        listView = findViewById(R.id.lv);
        //test
        springView = findViewById(R.id.spring);
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            data.add("aaaaaaaaaaa" + i);
        }
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, data);
        listView.setAdapter(arrayAdapter);
        //        pullToRefreshListView.setAdapter(arrayAdapter);


        springView.setHeader(new MyHead());
        springView.setFooter(new MyHead());

        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                showToast("onRefresh");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        springView.onFinishFreshAndLoad();
                        notifyAPP();
                    }
                }, 1000);
            }


            @Override
            public void onLoadmore() {
                showToast("onLoadmore");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        springView.onFinishFreshAndLoad();
                    }
                }, 1000);
            }
        });

    }

    private void notifyAPP() {
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//        notification = new Notification();
        PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this, MainActivity.class), 0);
        notification = new Notification.Builder(MainActivity.this)
                .setAutoCancel(true)
                .setContentTitle("")
                .setContentText("")
                .setContentIntent(contentIntent)//pendingIntent
                .setSmallIcon(R.drawable.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .build();
        notification.icon = android.R.drawable.stat_sys_download;
        // notification.icon=android.R.drawable.stat_sys_download_done;
        notification.tickerText = getString(R.string.app_name) + "更新";
        notification.when = System.currentTimeMillis();
        notification.defaults = Notification.DEFAULT_LIGHTS;
        // 设置任务栏中下载进程显示的views
        remoteViews = new RemoteViews(getPackageName(), R.layout.notification_install_apk);
        notification.contentView = remoteViews;

        // TODO: 2018/5/17

//        notification.setLatestEventInfo(UpdateService.this, "", "", contentIntent);


        // 将下载任务添加到任务栏中
        nm.notify(notificationId, notification);
    }


    private void showToast(String content) {
        Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
    }

    private void test(PullToRefreshListView pullToRefreshListView) {
        TextView textView = new TextView(this);
        textView.setText("aaaaaaaaaaaaaaaaaaaaaaa");
        ListView refreshableView = pullToRefreshListView.getRefreshableView();
        refreshableView.getHeaderViewsCount();
        View childAtIndex = pullToRefreshListView.getChildAtIndex(0);
        Log.e("ggg", "childAtIndex = " + childAtIndex.getClass().getSimpleName());
        ILoadingLayout loadingLayoutProxy = pullToRefreshListView.getLoadingLayoutProxy();
        pullToRefreshListView.getRefreshableView().addHeaderView(textView);
    }
}
